<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>销售策略列表</title>
    <script type="text/javascript">
      var newSalesStrategyWin;
      var editSalesStrategyWin;
      var salesStrategyId;
      var salesStrategyAllInfoStore;
      //处理销售类型锁定
      function lockSalesStrategyFn(id){
        Ext.Ajax.request({
          url : '/sales_strategy/salesStrategyLock',
          success : function (result, request) {
            salesStrategyAllInfoStore.load();
          },
          failure : function (result, request){
            Ext.MessageBox.show({
              title: '消息',
              msg: "通讯失败，请从新操作",
              buttons: Ext.MessageBox.OK,
              icon: Ext.MessageBox.WARNING
            });
          },
          method : 'GET',
          params : {
            id : id
          }
        });
      }
      Ext.onReady(function(){
        //销售策略设置---------------------------------------------------------
        //生成有关销售策略的Grid
        salesStrategyAllInfoStore =  Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'SaleStrategy',
          proxy : {
            type : 'ajax',
            url : '/sales_strategy/getAllSalesStrategies',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          },
          //pageSize : pageSize,
          autoLoad : true
        });

        //锁定销售类型
        function renderSalesStrategyIsLucked(value, cellmeta, record, index, columnIndex, store){
          if (record.get("is_locked")=="1"){
            return "<a style=cursor:pointer onclick=lockSalesStrategyFn(" + store.getAt(index).get('id')+")><em style=color:red>锁定</em></a>";
          }else{
            return "<a style=cursor:pointer onclick=lockSalesStrategyFn(" + store.getAt(index).get('id')+")><em style=color:green>未锁定</em></a>";
          }
        }
        
        var salesStrategyGrid =  Ext.create('Ext.grid.Panel', {
          store: salesStrategyAllInfoStore,
          columns : [Ext.create('Ext.grid.RowNumberer'),{
            header: 'id',
            dataIndex: 'id',
            width: 30
          }, {
              header: '所属公司',
              dataIndex: 'company_name',
              width:100
          }, {
            header: '名称',
            dataIndex: 'name',
            width:190
          }, {
            header: '备注',
            dataIndex: 'description',
            width:430
          },{
            header: '操作员',
            dataIndex: 'employee_name',
            width: 55
          }, {
            header: '是否锁定',
            dataIndex: 'is_locked',
            renderer: renderSalesStrategyIsLucked,
            width: 60
          },{
            header: '生成日期',
            dataIndex: 'created_at',
            renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
            width: 115
          },{
            header: '修改日期',
            dataIndex: 'updated_at',
            renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
            width: 115
          }],
          selModel :Ext.create('Ext.selection.CheckboxModel'),
          width: screenWidth-165,
          height: screenHeight-285,
          iconCls: 'icon-grid',
          loadMask: true,
          //collapsible: true,
          //animCollapse: false,
          //frame: true,
          //title: '销售策略列表',
          bbar: Ext.create('Ext.toolbar.Paging', {
            pageSize: 10,
            store: salesStrategyAllInfoStore,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          }),
          tbar: [{
              text: '添加',
              iconCls: 'addItem',
              handler : function(){
                newSalesStrategyWin = Ext.create('Ext.window.Window', {
                  layout: 'fit',
                  width:327,
                  height:215,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  //modal: true,
                  title: '添加销售策略',
                  autoLoad: {
                    url: "/sales_strategy/newSalesStrategy",
                    scripts: true
                  }
                });
                newSalesStrategyWin.on("destroy",function(){
                  salesStrategyAllInfoStore.load();
                });
                newSalesStrategyWin.resizable = false;
                newSalesStrategyWin.show();
              }
            },"-",{
              text: '编辑',
              iconCls: 'editItem',
              handler : function(){
                var records = salesStrategyGrid.getSelectionModel().getSelection();
                if(records.length==0){
                  Ext.MessageBox.show({
                    title: '提示信息',
                    msg: "请先选中一条记录后，再编辑。",
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.WARNING
                  });
                }else{
                  //把表单添加到窗口中
                  salesStrategyId = records[0].get('id');
                  editSalesStrategyWin = Ext.create('Ext.window.Window', {
                    title: '编辑销售策略',
                    layout:'fit',
                    width:327,
                    height:215,
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    autoLoad: {
                      url: "/sales_strategy/editSalesStrategy?id=" + salesStrategyId,
                      scripts: true
                    }
                  });
                }
                editSalesStrategyWin.on("destroy",function(){
                  salesStrategyAllInfoStore.load();
                });
                editSalesStrategyWin.resizable = false;
                editSalesStrategyWin.show();
              }
            }]
        });
        salesStrategyGrid.render('sales_strategy_list');
        salesStrategyAllInfoStore.load();
      })
    </script>
  </head>
  <body>
    <div id="sales_strategy_list"></div>
  </body>
</html>
